package com.shunlin.paopaoku.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.shunlin.paopaoku.box2d.*;
import com.shunlin.paopaoku.map.*;

import static com.shunlin.paopaoku.box2d.Box2DVars.*;
import static com.shunlin.paopaoku.box2d.Box2DVars.width;

//游戏的主类

public class MyGame extends ApplicationAdapter {
    private Box2DWorld b2dWorld;
    private GameMap gameMap;
    private Box2DSprite b2dSprite;
    private float deltaTime = 0f;
    private SpriteBatch sb;
    private GameButton button;
    private Stage stage;
    private Music bgMusic;

    @Override
    public void create() {
        b2dWorld = ClassManager.b2dWorld;
        gameMap = new GameMap("map/1.tmx");

        b2dWorld.makeGround(new TmxMapLoader().load("map/1.tmx"));
        b2dWorld.makeStar(new TmxMapLoader().load("map/1.tmx"));
        b2dWorld.setObjectPosition(new TmxMapLoader().load("map/1.tmx"));
        b2dWorld.makePlayer();

        b2dSprite = new Box2DSprite(b2dWorld.body);

        sb = new SpriteBatch();

        button = new GameButton();

        stage = new Stage();

        button.left.setPosition(50, 50);
        button.right.setPosition(200, 50);
        button.jump.setPosition(width - 400, 50);

        stage.addActor(button); //往一个舞台里面放入演员
        stage.addActor(button.left);
        stage.addActor(button.right);
        stage.addActor(button.jump);

        Gdx.input.setInputProcessor(stage); //设置游戏的监听

        bgMusic = GameSound.makeMusic("sound/bg.mp3");
        bgMusic.setLooping(true);
        bgMusic.setVolume(1);
        bgMusic.play();
    }

    @Override
    public void render() //这个类会自动的循环调用
    {
        Gdx.gl20.glClear(Gdx.gl20.GL_COLOR_BUFFER_BIT);
        Gdx.gl20.glClearColor(0, 0, 0, 0);

        gameMap.drawMap();
        gameMap.renderMap();

        b2dWorld.update();

        deltaTime += Gdx.graphics.getDeltaTime(); //拿到一个delta时间,用于设置渲染间隔

        sb.setProjectionMatrix(gameMap.mapCamera.combined); //设置画笔的矩阵

        b2dSprite.render(sb, deltaTime); //调用Box2DSpirit类里边的,render方法

        stage.act();
        stage.draw();
    }
}
